var timeFn=null;

var defaultData={
    fixMenuTreeNodeParentId:null
}


$(function(){
			$('#cc').layout();
			setHeight();

//			initForm();

			$('#menuTree').tree({
                    url: '/baseMenu/tree',
                    method: 'get',
                    loadFilter: function(data){
                            if (data.d){
                                return data.d;
                            } else {
                                return data;
                            }
                        },
                    onClick: function(node){
                        clearTimeout(timeFn);
                        timeFn=setTimeout(function(){
                    		clickTreeNode(this,node);
                        },300);
                    },
                    onDblClick: function(node){
                        clearTimeout(timeFn);
                        dblClickTreeNode(this,node);
                    }

                });
		});

function setHeight(){
    var c = $('#cc');
    var p = c.layout('panel','center');	// get the center panel
    var oldHeight = p.panel('panel').outerHeight();
    p.panel('resize', {height:'auto'});
    var newHeight = p.panel('panel').outerHeight();
    c.layout('resize',{
        height: (c.height() + newHeight - oldHeight)
    });
    }

/*
    点击菜单节点，将其数据填到表单内
*/
function clickTreeNode(tree,node){
    var nodeId=node.id;
    defaultData.fixMenuTreeNodeParentId=node.parentId;
    apiMenuManageGetById(nodeId,function(res){
        if(res.status == 200){
            var resMenu=res.data;
            $('#ff').form('load',resMenu);
        }else{
            var tip=getMessagerTip("ok",'获取菜单数据失败，请重试。');
            messager('失败',tip,4000,'info');
        }
    });
}

/*
    双击菜单树节点
*/
function dblClickTreeNode(tree,node){
    var nodeId=node.id;
    var nodeText=node.text;
    var parentNameI=$("input[name='parentName']");

    $("#parentName").textbox('setValue',nodeText);
    $("#parentId").textbox('setValue',nodeId);
}

function clearForm(){
    $('#ff').form('clear');
}

/*
    切换提交按钮为添加
*/
function addItem(){
//    openForm();
    $("#submitEditButton").hide();
    $("#submitButton").show();
}

/*
    切换提交按钮为编辑
*/
function editItem(){
//    openForm();
    $("#submitEditButton").show();
    $("#submitButton").hide();
}

/*
    添加菜单信息
*/
function submitForm(){
    var checkResult=checkValidateForm($("#ff"));

    if(checkResult){
        var fields = $("#ff").find("input").serializeArray();
        var dataJson=convertFormDataToJson(fields);
        delete dataJson.id;

        apiMenuManageAddMenuData(JSON.stringify(dataJson),function(res){
            if(res.status==200){
                var tip=getMessagerTip("ok",res.message);
                messager('成功',tip,4000,'info');

                $('#menuTree').tree('reload');
            }else{
                var tip=getMessagerTip("no",res.message);
                messager('失败',tip,4000,'info');
            }
        });
    }
}

/*
    编辑菜单信息
*/
function submitEditForm(){
    var checkResult=checkValidateForm($("#ff"));

    if($("#parentId").val() != defaultData.fixMenuTreeNodeParentId){
        messagerAlert('警告','不能修改父级节点','warning');
        return;
    }

    if(checkResult){
        var fields = $("#ff").find("input").serializeArray();

        var data=convertFormDataToJsonStr(fields);

        apiMenuManageEditMenuData(data,function(res){
            if(res.status==200){
                var tip=getMessagerTip("ok",res.message);
                messager('成功',tip,4000,'info');

                $('#menuTree').tree('reload');
            }else{
                var tip=getMessagerTip("no",res.message);
                messager('失败',tip,4000,'info');
            }
        });
    }
}

function removeItem(){
    var treeNode = $("#menuTree").tree("getSelected");

    var chileren=$("#menuTree").tree('getChildren',treeNode);

    $.messager.confirm('确认','您确认想要删除该菜单吗？',function(r){
            if (r){
                var id=treeNode.id;
                apiMenuManageDelById(id,function(res){
                    if(res.status==200){
                        var tip=getMessagerTip("ok",res.message);
                        messager('成功',tip,4000,'info');

                        $('#menuTree').tree('reload');
                    }else{
                        var tip=getMessagerTip("no",res.message);
                        messager('失败',tip,4000,'info');
                    }
                });
            }
        });
}

//function ajaxSubmit({
//    headers:
//    {
//        Authorization: "123456789",
//        Content-Type: "application/json"
//    },
//    success: function (data) {
//        alert(data);
//    },
//    error: function (error) {
//        alert(error);
//    }
//})

//{
//	$('#ff').form('submit');
//}